import {ethers} from "ethers"

const ALCHEMY_MAINNET_URL = 'https://eth-mainnet.g.alchemy.com/v2/mkM5-GVJCs00ghRsjT7aU';
// 连接主网 provider
const provider = new ethers.JsonRpcProvider(ALCHEMY_MAINNET_URL);

// USDT的合约地址
const contractAddress = '0xdac17f958d2ee523a2206206994597c13d831ec7'

// 交易所地址
const accountBinance = '0x28C6c06298d514Db089934071355E5743bf21d60'

// 构建ABI
const abi = [
  "event Transfer(address indexed from, address indexed to, uint value)",
  "function balanceOf(address) public view returns(uint)",
];

// 创建合约对象
const contract = new ethers.Contract(contractAddress, abi, provider)

// 读取交易所地址余额
const balanceUSDT = await contract.balanceOf(accountBinance)
console.log(ethers.formatUnits(ethers.getBigInt(balanceUSDT), 6))

// 创建过滤器监听USDT转移进交易所
let filterBinanceIn = contract.filters.Transfer(null, accountBinance);
contract.on(filterBinanceIn, (res)=>{
    console.log('---------监听USDT转入交易所--------');
    console.log(`${res.args[0]} -> ${res.args[1]} ${ethers.formatUnits(res.args[2],6)}`)
})

// 创建过滤器监听USDT转出交易所
let filterBinanceOut = contract.filters.Transfer(accountBinance)
contract.on(filterBinanceOut, (res)=>{
    console.log('---------监听USDT转出交易所--------');
    console.log(`${res.args[0]} -> ${res.args[1]} ${ethers.formatUnits(res.args[2],6)}`)
})